#!/bin/bash

# This file is part of Hydra Paste

# USAGE:
# ./deploy      # Deploys scripts to TARGET_DIR
# ./deploy -y   # Skips confirmation prompt

# Script-wide variables
CURRENT_DIR=`pwd`
DOC="$CURRENT_DIR/doc"
LIB="$CURRENT_DIR/lib"
STATIC="$CURRENT_DIR/static"
# Where to send our files
TARGET_DIR="/var/www/p"


# Use -y to disable prompt; this feature is for users who know what they're doing
if [[ $1 != '-y' ]]
then
    echo "Are you sure you want to deploy the server?"
    echo "Please note that doing this on a dev machine will delete the /static/ directory"
    echo "Make sure that you know what you're doing!"
    select yn in "Yes" "No"; do
        case $yn in
            Yes ) break;;
            No ) exit -1;;
        esac
    done
fi

# Asking for user confirmation

echo "Deploying files to \"$TARGET_DIR\""
echo

echo "# Static Files #############################"

echo "Removing existing static files"
rm -r "$TARGET_DIR/static/"
mkdir "$TARGET_DIR/static/"

for FILE in `ls $STATIC`
do
    EXTENSION="${FILE##*.}"

    # TODO: For this section, minimize JS and CSS files -- linux-t
    if [ $EXTENSION == "js" ] || [ $EXTENSION == "css" ]
    then
        echo "$FILE is a $EXTENSION file"
    fi
    # END TODO

    echo "Deploying $STATIC/$FILE to $TARGET_DIR/static/$FILE"
    cp -r "$STATIC/$FILE" "$TARGET_DIR/static/$FILE"
done

echo "Regular Static Files Deployed!"
echo

echo '# Documentation ############################'

echo "Removing Existing Documentation"
rm -rf "$TARGET_DIR/doc/"
# Generate New Documenation
echo "Generating Sphinx documentation..."
cd "$CURRENT_DIR/doc/"
make html
cd "$CURRENT_DIR"
echo "Documentation Generated!"

echo "Deploying Documenation..."
cp -r "$DOC/build/html" "$TARGET_DIR/doc"
echo "Documentation Deployed!"
echo

echo "Deploy Done! Now run \`./hydrapaste.py\`!"

exit 0
